<template>
	<view>
		<!-- <cu-custom bgColor="bg-gradual-blue" :isBack="true" >
		<block slot="content">编辑</block>
	</cu-custom> -->
		<view v-if="page">
			<form @submit="formSubmit">

				<input type="text" style="display: none;" name="_id" :value="data._id" />
				<!-- <view class="cu-form-group align-start">
					<view class="title">标题</view>
					<textarea :value="data.title" name="title" maxlength="-1" placeholder="请输入标题"></textarea>
				</view>
				<view class="cu-form-group align-start">
					<textarea :value="data.content" name="content" maxlength="-1" placeholder="请输入内容"></textarea>
				</view> -->
				<!-- {{data}} -->
				<view class="bg-white">
					<upimg-box @callParent="callImgList" :defaultImgsList="data.imgList" :defaultVideo="data.srcVideo || ''"></upimg-box>
					<label class="radio" v-if="userInfo.username==123456">
						<view>审核状态</view>
						<switch :checked="data.status==0?false:true" name="status" @change="change" color="#000000" />
						{{data.status==1?'已通过审核':'未通过审核'}}
						{{kaiguan}}
					</label>
					<!-- <button type="primary" form-type="submit">测试值</button> -->
					<view class="padding flex flex-direction">
						<button class="cu-btn bg-blue shadow lg" form-type="submit">提交</button>
					</view>
				</view>
			</form>
		</view>
	</view>
</template>

<script>
	import mdate from "@/common/date.js";
	import upimgBox from "../../components/upimgbox.vue";
	export default {
		components: {
			upimgBox
		},
		data: function() {
			return {
				imgsList: [],
				id: "",
				data: {},
				page: false,
				ssuserid: uni.getStorageSync('userInfo')._id || this.$store.getters.userInfo._id,
				uname: uni.getStorageSync('userInfo').username || this.$store.getters.userInfo.username,
				kaiguan: "",
				userInfo:{},
			}
		},
		onLoad: function(ops) {
			this.userInfo = uni.getStorageSync('userInfo')
			this.id = ops.id
			this.getPage()
		},
		methods: {
			change(e) {
				console.log('switch1 发生 change 事件，携带值为', e.target.value)
				this.kaiguan = e.target.value;
			},

			callImgList: function(e) {
				this.imgsList = e;
			},
			// 获取图片
			getPage: async function() {
				var that = this
				if (that.uname == 123456) {
					uniCloud.callFunction({
						name: "bbs_topic",
						data: {
							cloudAction: "add2",
							params: {
								ssuserid: this.ssuserid,
								id: this.id
							}
						}
					}).then((result) => {
						var res = result.result
						if (res.error) {
							uni.showToast({
								title: res.message
							})
							return false
						}
						that.page = true
						that.data = res.data.data[0]
						// console.log(that.data) 
					})
					return
				}
				uniCloud.callFunction({
					name: "bbs_topic",
					data: {
						cloudAction: "add",
						params: {
							ssuserid: this.ssuserid,
							id: this.id
						}
					}
				}).then((result) => {
					var res = result.result
					if (res.error) {
						uni.showToast({
							title: res.message
						})
						return false
					}
					that.page = true
					that.data = res.data.data[0]
					// console.log(that.data) 
				})
			},
			// 更改,无权限限制
			// formSubmit2:function(e){
			// 	var params=e.detail.value;
			// 	console.log('未更改'+params.status);
			// 	if(params.status==true){
			// 		params.status=1
			// 		console.log('更改成功');

			// 	}
			// 	else{
			// 		params.status=0
			// 	}
			// 	console.log('更改后的status'+params.status);
			// },
			formSubmit: function(e) {

				if (this.imgsList.length == 0) {
					this.imgsList = this.data.imgList
				}

				var params = e.detail.value;
				console.log(params);
				if (params.status == true) {
					params.status = 1
					console.log('更改成功1或0成功');

				} else {
					params.status = 0
				}
				params.createTime = new Date().Format("yyyy-MM-dd hh:mm:ss");
				params.imgList = this.imgsList;
				params.ssuserid = this.ssuserid;
				params.uname = this.uname;


				uniCloud.callFunction({
					name: 'bbs_topic',
					data: {
						cloudAction: "save2",
						params: params
					}
				}).then((result) => {
					var res = result.result;
					if (res.error) {
						uni.showToast({
							title: res.message
						})
						return false;
					}
					uni.showToast({
						title: "发布成功",
						icon: 'none'
					})
					uni.navigateBack()
				}).catch((err) => {
					console.error(err)
				})



				// uniCloud.callFunction({
				// 	name: 'bbs_topic',
				// 	data: {
				// 		cloudAction:"save",
				// 		params:params
				// 	}
				// }).then((result) => {
				// 	var res=result.result;
				// 	if(res.error){
				// 		uni.showToast({
				// 			title:res.message
				// 		})
				// 		return false; 
				// 	}
				// 	uni.showToast({
				// 		title:"发布成功",
				// 		icon:'none'
				// 	}) 
				// 	uni.navigateBack()
				// }).catch((err) => {				 
				// 	console.error(err)
				// })
			}
		}
	}
</script>

<style lang="scss" scoped>

</style>
